import { Controller, Get, Post, Body, Param, Delete, UseGuards, NotFoundException } from '@nestjs/common';
import { TimerRecordsService } from './timer-records.service';
import { CreateTimerRecordDto } from './dto/create-timer-record.dto';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';

@Controller('timer-records')
export class TimerRecordsController {
  constructor(private readonly timerRecordsService: TimerRecordsService) {}

  @Post()
  @UseGuards(JwtAuthGuard)
  create(@Body() createTimerRecordDto: CreateTimerRecordDto) {
    return this.timerRecordsService.create(createTimerRecordDto);
  }

  @Get()
  @UseGuards(JwtAuthGuard)
  findAll() {
    return this.timerRecordsService.findAll();
  }

  @Get('user/:userId')
  @UseGuards(JwtAuthGuard)
  findByUserId(@Param('userId') userId: string) {
    return this.timerRecordsService.findByUserId(parseInt(userId));
  }

  @Get('employee/:employeeId')
  @UseGuards(JwtAuthGuard)
  async findByEmployeeId(@Param('employeeId') employeeId: string) {
    try {
      const records = await this.timerRecordsService.findByEmployeeId(employeeId);
      console.log(`查询员工 ${employeeId} 的记录，找到 ${records ? records.length : 0} 条记录`);
      return records || [];
    } catch (error) {
      console.error(`Error fetching records for ${employeeId}:`, error);
      throw error;
    }
  }

  @Get(':id')
  @UseGuards(JwtAuthGuard)
  findOne(@Param('id') id: string) {
    return this.timerRecordsService.findOne(parseInt(id));
  }

  @Delete(':id')
  @UseGuards(JwtAuthGuard)
  remove(@Param('id') id: string) {
    return this.timerRecordsService.remove(parseInt(id));
  }
}